package cn.yoyo.controller.fore;

import cn.yoyo.entity.*;
import cn.yoyo.service.CartService;
import cn.yoyo.service.OrderService;
import cn.yoyo.tools.GetUser;
import cn.yoyo.tools.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class CartController {
    @Autowired
    CartService cartService;
    @Autowired
    OrderService orderService;

    @RequestMapping("/api/fore/cart/findAll")
    public Map<String, Object> findAll() {
        SysUser user = GetUser.getUser();
        return RespJson.pack(200, "", cartService.findAll(user.getId()));
    }

    @RequestMapping("/api/fore/cart/addCart2Order")
    public Map<String, Object> addCart2Order(@RequestBody List<Item> items) {
        return RespJson.pack(200, "下单成功", orderService.add(items));
    }

    @RequestMapping("/api/fore/cart/add2Cart")
    public Map<String, Object> add2Cart(Integer bookid) {
        cartService.add(bookid);
        return RespJson.pack(200, "成功添加到购物车", null);
    }

    @RequestMapping("/api/fore/cart/delete/{cartid}")
    public Map<String, Object> delete(@PathVariable("cartid") Integer id) {
        cartService.delete(id);
        SysUser user = GetUser.getUser();
        return RespJson.pack(200, "删除成功", cartService.findAll(user.getId()));
    }
}
